module.exports = {
    GachaBingo : 7,              // 抽到头奖最大数
    GachaMax : 9,            // 抽奖最大数量 
    // 抽蛋
    getGachaid(id){
        let gacha_time = he.local.getJson("gacha","user")
        let spec_gacha_list = []

        if(gacha_time > 0){
            let current_gacha = he.db.getDataListByArgs("Gacha_Spc",{ gachaId:id , gacha : gacha_time  })
            let loop_gacha = he.db.getDataListByArgs("Gacha_Spc",{ gachaId:id , gacha : -1  })
            for(let i = 0;i < current_gacha.length;i++){
                spec_gacha_list.push(current_gacha[i])
            }
            for(let i = 0;i < loop_gacha.length;i++){
                spec_gacha_list.push(loop_gacha[i])
            }
        }else{
            let first_gacha = he.db.getDataListByArgs("Gacha_Spc",{ gachaId:id , gacha : 0 })
            for(let i = 0;i < first_gacha.length;i++){
                spec_gacha_list.push(first_gacha[i])
            }
        }
        let base_gacha = he.db.getDataListByArgs("Gacha_Res",{ gachaId:id })
        base_gacha.sort((a,b) => {
            return a.order - b.order
        })
        

        let spec_res_list = []
        for(let i = 0;i < spec_gacha_list.length;i++){
            let spec_info = spec_gacha_list[i]
            let spec_info_arr = spec_gacha_list[i].list.split("#")
            
            if(spec_info.num == 0){
                for(let j = 0;j < spec_info_arr.length;j++){
                    spec_res_list.push(spec_info_arr[j])
                }
            }else{
                for(let j = 0;j < spec_info.num;j++){
                    let spec_index = parseInt(Math.random() * spec_info_arr.length)
                    spec_res_list.push(spec_info_arr[spec_index])
                }
            }
        }

        let gacha_res_list = []
        let specMax = this.GachaMax - this.GachaBingo
        let gacha_ran_max = 0
        let multiplying_power_list=[]
        for(let i = 0;i < base_gacha.length;i++){
            gacha_ran_max += base_gacha[i].probability
        }
        for(let i = 0;i < this.GachaBingo;i++){
            let randomPro = Math.random() * gacha_ran_max
            for(let j = 0; j < base_gacha.length; j++){
                randomPro -= base_gacha[j].probability
                if(randomPro < 0){
                    he.log(base_gacha[j].item)
                    gacha_res_list.push(base_gacha[j].item)
                    let multiplying_power=this.GetMultiplyingPower(base_gacha[j])
                    multiplying_power_list.push(multiplying_power)
                    break
                }
            }
        }
        let topReward = spec_res_list[0]
        if(specMax > spec_res_list.length){
            for(let i = 0; i < specMax; i++){
                let randomPecPro = parseInt(specMax * Math.random())
                if(spec_res_list.length && (randomPecPro > spec_res_list.length || (spec_res_list.length + gacha_res_list.length == this.GachaMax))){
                    let spec = spec_res_list.pop()
                    gacha_res_list.push(spec)
                    multiplying_power_list.push(1)
                }else{
                    let randomPro = Math.random() * gacha_ran_max
                    for(let j = 0; j < base_gacha.length; j++){
                        randomPro -= base_gacha[j].probability
                        if(randomPro < 0){
                            gacha_res_list.push(base_gacha[j].item)
                            let multiplying_power=this.GetMultiplyingPower(base_gacha[j])
                            multiplying_power_list.push(multiplying_power)
                            break
                        }
                    }
                }

            }
            // 最大值 随机 如果大于长度 普通 往前加空
            // 如果小于长度 往后加
        }else{
            for(let i = 0;i < specMax;i++){
                gacha_res_list.push(spec_res_list[i])
                multiplying_power_list.push(1)
            }
        }

        //修改道具数量
        gacha_res_list= this. ModifyTheNumberOfGacha(multiplying_power_list,gacha_res_list)
        //传递箱子等级
        let myGachaLevel = this.GetGaChaLevel(id)
        if(gacha_time){
            gacha_time += 1
        }else{
            gacha_time + 1
        }
        he.local.setJson("gacha",gacha_time,'user')
        return {
            list : gacha_res_list,          // 顺序
            top : topReward,                // 头奖
            mplist : multiplying_power_list,// 倍率
            gachaLevel : myGachaLevel       // 箱子等级
        }
    },

    //计算暴击倍数
    GetMultiplyingPower(base_gacha){
        //console.log("双倍概率"+base_gacha.doubleProbability);
        let randomNumber =  Math.floor(Math.random()*100)//随机数
        //console.log("随机数："+randomNumber)
        if(randomNumber <= (100*base_gacha.tripleProbability) ){
            return 3
        }
        else if (randomNumber<=(100*base_gacha.doubleProbability)) {
            return 2
        }
        else{
            return 1
        }
    },
    //改变扭蛋打开后的道具数量
    ModifyTheNumberOfGacha( multiplying_power_list , gacha_res_list ){

        let new_lsit = [] = gacha_res_list

        for (let i = 0; i < multiplying_power_list.length; i++) {

            let value = multiplying_power_list[i];
            // console.log("第"+i+"个值为"+new_lsit[i]+"现在翻"+value+"倍")
            if(value != 1){
                // console.log("第"+i+"个值为"+new_lsit[i]+"现在翻"+value+"倍")
               let item = ""
               let str =[] = new_lsit[i].split("_")
               item=str[0]+"_"+(str[1]*value)
                // console.log(item)
               new_lsit[i]=item
            }
        }
        return new_lsit
    },
    GetGaChaLevel(id){
        return id % 5000
    }

}